
package cfar.ff.interfaces;

import cfar.ff.fun.F1;
import cfar.ff.fun.F2;

import java.util.Iterator;
//import static cfar.ff.fun.F1W.$;

/**
 *
 * @author badi
 */
public abstract class Monoid<A> {
    public abstract A empty();
    public abstract F2<A,A,A> append();
    public abstract A fromMonoid();

    public F1<Iterable<Monoid<A>>, A> concat(){
        return new F1<Iterable<Monoid<A>>, A>() {

            public A f(Iterable<Monoid<A>> seq) {
                Iterator<Monoid<A>> i = seq.iterator();
                Monoid<A> first = i.next();
                F2<A,A,A> f = first.append();
                A a = first.fromMonoid();
                while(i.hasNext())
                    a = f.f(a, i.next().fromMonoid());
                return a;
            }
        };
    }
}
